package org.example.controller;


import org.example.pojo.Food;
import org.example.pojo.FoodTag;
import org.example.pojo.Result;
import org.example.service.FoodService;
import org.example.service.Impl.FoodServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/food")
public class FoodController {
    private final FoodService foodService;

    @Autowired
    public FoodController(FoodService foodService) {
        this.foodService = foodService;
    }

    /**
     * 获取所有食品列表
     */
    @GetMapping
    public Result getAllFoods() {
        List<Food> foods = foodService.findAll();
        return Result.success(foods);
    }

    /**
     * 根据ID查询食品
     */
    @GetMapping("/{id}")
    public Result getFoodById(@PathVariable Long id) {
        Food food = foodService.findById(id);
        return Result.success(food);
    }

    /**
     * 添加新食品
     */
    @PostMapping
    public Result addFood(@RequestBody Food food) {
        Food savedFood = foodService.addFood(food);
        return Result.success(savedFood);
    }

    /**
     * 删除指定食品
     */
    @DeleteMapping("/{id}")
    public Result deleteFood(@PathVariable Long id) {
        foodService.deleteById(id);
        return Result.success();
    }

    /**
     * 获取指定食品的标签列表
     */
    @GetMapping("/{id}/tags")
    public Result getFoodTags(@PathVariable Long id) {
        List<FoodTag> tags = foodService.findTagsByFoodId(id);
        return Result.success(tags);
    }

    /**
     * 为食品添加新标签
     */
    @PostMapping("/{id}/tags")
    public Result addTagToFood(
            @PathVariable Long id,
            @RequestBody FoodTag tag) {
        // 设置食品ID关联关系
        tag.setFoodId(id);
        FoodTag savedTag = foodService.addTag(tag);
        return Result.success(savedTag);
    }
}
